//
//  TipInfoToolBar.m
//  TipsProject
//
//  Created by Suny on 15/6/4.
//  Copyright (c) 2015年 Suny. All rights reserved.
//

#import "TipInfoToolBar.h"


@implementation TipInfoToolBarItem

- (UILabel *)titleLabel
{
	if (!_titleLabel)
 {
		_titleLabel = [[UILabel alloc] init];
		_titleLabel.font = [UIFont systemFontOfSize:12];
	}
	return _titleLabel;
}

- (void)setSelected:(BOOL)selected
{
	_selected = selected;
	UIImage *image = [UIImage imageNamed:(_selected?self.selectedImageName:self.normalImageName)];
	
	UIImageView *imageView = (UIImageView *)[self viewWithTag:4009];
	if(image)
		imageView.image = image;
}

+ (TipInfoToolBarItem *)itemWithTitle:(NSString *)title 
							 andImageName:(NSString *)imageName
				 andSelectedImageName:(NSString *)selectedImageName
							  andSize:(CGSize)size
{
	TipInfoToolBarItem *item = [TipInfoToolBarItem boxWithSize:size];
	[item setMargin:UIEdgeInsetsMake(10, 10, 10, 10)];
	item.title = title;
	item.image = [UIImage imageNamed:imageName];
	item.normalImageName = imageName;
	item.selectedImageName = selectedImageName;
	item.userInteractionEnabled = YES;
	
	__weak TipInfoToolBarItem *weakitem = item;
	item.asyncLayoutOnce = ^{			
			// got the photo, so lets show it
		dispatch_async(dispatch_get_main_queue(), ^{
			__strong TipInfoToolBarItem *strongItem = weakitem;
			UIImageView *imageView = [[UIImageView alloc] init];
			imageView.tag = 4009;
			[strongItem addSubview:imageView];
			imageView.size = CGSizeMake(48, 48);
			if (CHECK_VALID_STRING(title))
			{
				
				UILabel *label = weakitem.titleLabel;
				[strongItem addSubview:label];
				
				label.size = weakitem.size;
				label.alpha = 1;
				label.backgroundColor = [UIColor clearColor];
				label.textAlignment = NSTextAlignmentCenter;
				label.text = strongItem.title;
				[label sizeToFit];
				label.centerX = strongItem.width/2.0;
				label.top = strongItem.bottom - label.height - 10;
				
				label.autoresizingMask = UIViewAutoresizingFlexibleWidth
				| UIViewAutoresizingFlexibleHeight;
				
				imageView.height -= label.height;
				imageView.centerX  =  strongItem.width/2.0;
				
			}
			imageView.alpha = 0;
			imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth
			| UIViewAutoresizingFlexibleHeight;
			
			
			[imageView setImage:[UIImage imageNamed:imageName]];
			
			
			// fade the image in
			[UIView animateWithDuration:0.2 animations:^{
				imageView.alpha = 1;
			}];

		});
							
	};
	
	return item;
}
				

+ (TipInfoToolBarItem *)itemForAbstarctWithTitle:(NSString *)title 
							 andImage:(UIImage *)image
							  andSize:(CGSize)size
{
	TipInfoToolBarItem *item = [TipInfoToolBarItem boxWithSize:size];
	item.backgroundColor = [UIColor clearColor];
	item.title = title;
	item.image = image;
	
	__weak TipInfoToolBarItem *weakitem = item;
	item.asyncLayoutOnce = ^{			
		// got the photo, so lets show it
		dispatch_async(dispatch_get_main_queue(), ^{
			__strong TipInfoToolBarItem *strongItem = weakitem;
			UIImageView *imageView = [[UIImageView alloc] init];
			[strongItem addSubview:imageView];
			imageView.frame = CGRectMake(12, 5, 12, 12);
			if (CHECK_VALID_STRING(title))
			{
				
				UILabel *label = weakitem.titleLabel;
				[strongItem addSubview:label];
				
				label.size = weakitem.size;
				label.alpha = 1;
				label.backgroundColor = [UIColor clearColor];
				label.textAlignment = NSTextAlignmentCenter;
                label.font = [UIFont systemFontOfSize:14];
				label.text = strongItem.title;
				[label sizeToFit];
				label.left = imageView.right + 5;
				label.height = 30;
				label.centerY =  imageView.centerY;
				
				label.autoresizingMask = UIViewAutoresizingFlexibleWidth
				| UIViewAutoresizingFlexibleHeight;
				
			}
			imageView.alpha = 0;
			imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth
			| UIViewAutoresizingFlexibleHeight;
			
			
			[imageView setImage:image];
			
			
			// fade the image in
			[UIView animateWithDuration:0.2 animations:^{
				imageView.alpha = 1;
			}];
			
		});
		
	};
	
	return item;
}


					  
								  
@end
@interface TipInfoToolBar ()
@property (nonatomic, strong)UIView *lineView;
@end


@implementation TipInfoToolBar

- (id)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self)
	{

		[self addSubview:self.lineView];
	}
	return self;
}

- (UIView *)lineView
{
	if (!_lineView)
	{
		_lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, self.width - 20, 0.5)];
		_lineView.backgroundColor = [UIColor colorWithHexString:@"c5c5c5"];

	}
	
	return _lineView;
}


+ (TipInfoToolBar *)tipInfoToolBarWithAbstractList:(TipAbstract *)abstract andSize:(CGSize)size
{
	
	TipInfoToolBar *toolBar = [TipInfoToolBar boxWithSize:size];
	toolBar.contentLayoutMode = MGLayoutStackHorizontalWithWrap;
	toolBar.abstract = abstract;
	toolBar.backgroundColor = [UIColor clearColor];
    __weak TipInfoToolBar *weakBar = toolBar;

    TipInfoToolBarItem *like = [TipInfoToolBarItem itemForAbstarctWithTitle:abstract.praiseNum
														andImage:[UIImage imageNamed:@"2_1_like_icon_normal.png"]
														 andSize:CGSizeMake(size.width/3, size.height)]    ;
	like.tag = 2000;
	like.onTap = ^{
		[weakBar clickView:2000];

	};
	
	[toolBar.boxes addObject:like];

    TipInfoToolBarItem *comment = [TipInfoToolBarItem itemForAbstarctWithTitle:abstract.commentsNum
                                                                      andImage:[UIImage imageNamed:@"2_1_comment_icon_normal.png"]
                                                                       andSize:CGSizeMake(size.width/3, size.height)]    ;

    toolBar.lineView.hidden = YES;
    comment.tag = 1000;

    comment.onTap = ^{
        [weakBar clickView:1000];
    };

    [toolBar.boxes addObject:comment];

    TipInfoToolBarItem *save = [TipInfoToolBarItem itemForAbstarctWithTitle:abstract.collectNum
														andImage:[UIImage imageNamed:@"2_1_save_icon_normal.png"]
	 													 andSize:CGSizeMake(size.width/3, size.height)]    ;
	
	save.tag = 3000;
	save.onTap = ^{
		[weakBar clickView:3000];

	};
	
	[toolBar.boxes addObject:save];
	
	
	return toolBar;

}

- (void)clickView:(NSInteger)tag
{
	TipInfoToolBarItem *barItem = [self getToolBarItemViewByTag:tag];
//	barItem.titleLabel.text = [NSString stringWithFormat:@"%ld",[barItem.titleLabel.text integerValue]+1];
	if (!barItem.selected)
	{
		if ([self.toolBarDelegate respondsToSelector:@selector(tipInfoToolBar:clickView:)])
		{
			[self.toolBarDelegate tipInfoToolBar:self clickView:barItem];
		}
	}

}

+ (TipInfoToolBar *)tipInfoToolBarWithTipInfo:(TipInfo *)tipInfo andSize:(CGSize)size 
{
	TipInfoToolBar *toolBar = [TipInfoToolBar boxWithSize:size];
	toolBar.contentLayoutMode = MGLayoutStackHorizontalWithWrap;
	toolBar.tipInfo = tipInfo;
	
	__weak TipInfoToolBar *weakBar = toolBar;

	CGFloat itemwidth = toolBar.width/3.0 - 22;
	TipInfoToolBarItem *comment = [TipInfoToolBarItem itemWithTitle:tipInfo.commentsNum
														   andImageName:@"2_1_comment_icon_normal"
											   andSelectedImageName:@"2_1_comment_icon_selected"
                                                            andSize:CGSizeMake(itemwidth, size.height)]    ;
	comment.tag = 1000;
    comment.onTap = ^{
		[weakBar clickView:1000];

	};

    [toolBar.boxes addObject:comment];


    TipInfoToolBarItem *like = [TipInfoToolBarItem itemWithTitle:tipInfo.praiseNum
													andImageName:@"2_1_like_icon_normal"
											andSelectedImageName:@"2_1_like_icon_selected"
                                                            andSize:CGSizeMake(itemwidth, size.height)]    ;
	like.tag = 2000;
    like.onTap = ^{
		[weakBar clickView:2000];
	};

    [toolBar.boxes addObject:like];


    TipInfoToolBarItem *save = [TipInfoToolBarItem itemWithTitle:tipInfo.collectNum
													andImageName:@"2_1_save_icon_normal"
											andSelectedImageName:@"2_1_save_icon_selected"
                                                         andSize:CGSizeMake(itemwidth, size.height)]    ;
	save.tag = 3000;
  
	save.onTap = ^{
		[weakBar clickView:3000];
	};

    [toolBar.boxes addObject:save];

    return toolBar;
}

- (TipInfoToolBarItem *)getToolBarItemViewByTag:(NSInteger)tag
{
	return (TipInfoToolBarItem *)[self viewWithTag:tag];
}


- (void)reloadDataByTipInfo:(TipInfo *)tipInfo
{
	TipInfoToolBarItem *comment  = [self getToolBarItemViewByTag:1000];
	comment.title = tipInfo.commentsNum;
	TipInfoToolBarItem *like = [self getToolBarItemViewByTag:2000];
	like.title = tipInfo.praiseNum;
	TipInfoToolBarItem *save = [self getToolBarItemViewByTag:3000];
	save.title = tipInfo.collectNum;
}

- (void)reloadDataByAbstract:(TipAbstract *)abstract
{
	TipInfoToolBarItem *comment  = [self getToolBarItemViewByTag:1000];
	comment.title = abstract.commentsNum;
	TipInfoToolBarItem *like = [self getToolBarItemViewByTag:2000];
	like.title = abstract.praiseNum;
	TipInfoToolBarItem *save = [self getToolBarItemViewByTag:3000];
	save.title = abstract.collectNum;
	
}


@end
